package com.example.fmgidsprototype.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;

import com.example.fmgidsprototype.data.VenueReviewTable.ReviewColumns;
import com.example.fmgidsprototype.model.Review;

public class VenueReviewDao implements Dao<Review>{
	
	private SQLiteDatabase db;
	private SQLiteStatement insertStatement;
	private static final String INSERT = "insert or ignore into " + VenueReviewTable.TABLE_NAME + "(" +
			ReviewColumns.VENUE + ", " + ReviewColumns.COMMENT + ", " + ReviewColumns.DATE_CREATED + 
			", " + ReviewColumns.USER_NAME + ") values (?,?,?,?)";
	
	public VenueReviewDao(SQLiteDatabase db){
		this.db = db;
		insertStatement = db.compileStatement(VenueReviewDao.INSERT);
	}

	@Override
	public long save(Review review) {
		insertStatement.clearBindings();
		insertStatement.bindLong(1, review.getVenueId());
		insertStatement.bindString(2, review.getComment());
		insertStatement.bindLong(3, review.getTimestamp());
		insertStatement.bindString(4, review.getUserName());
		try{
			return insertStatement.executeInsert();
		}
		catch(Exception e){
			return -1;
		}
		
	}

	@Override
	public long update(Review review) {
		ContentValues values = new ContentValues();
		values.put(ReviewColumns.VENUE, review.getVenueId());
		values.put(ReviewColumns.COMMENT, review.getComment());
		values.put(ReviewColumns.DATE_CREATED, review.getTimestamp());
		values.put(ReviewColumns.USER_NAME, review.getUserName());
		try{
			return db.update(VenueReviewTable.TABLE_NAME, values, ReviewColumns.DATE_CREATED + " = ?", new String[]{
					String.valueOf(review.getTimestamp())});
		}
		catch(Exception e){
			return -1;
		}
	}

	@Override
	public void delete(Review review) {
		if(review.getId() > 0){
			db.delete(VenueReviewTable.TABLE_NAME, BaseColumns._ID + " = ?", 
					new String[]{String.valueOf(review.getId())});
		}
	}
	
	@Override
	public Review get(long id){
		return null;
	}

	public List<Review> getReviews(long id) {
		List<Review> reviews = new ArrayList<Review>();
		Cursor c = db.query(VenueReviewTable.TABLE_NAME, new String[]{
				BaseColumns._ID, ReviewColumns.VENUE, ReviewColumns.COMMENT,
				ReviewColumns.DATE_CREATED, ReviewColumns.USER_NAME
		}, ReviewColumns.VENUE + " = ?", new String[]{String.valueOf(id)}, null, null, null, null);
		if(c.moveToFirst()){
			do{
				Review review = createReviewFromCursor(c);
				if(review != null)
					reviews.add(review);
			}
			while(c.moveToNext());
		}
		if(!c.isClosed())
			c.close();
		return reviews;
	}

	@Override
	public List<Review> getAll() {
		// TODO Auto-generated method stub
		return null;
	}
	
	private Review createReviewFromCursor(Cursor c){
		Review review = null;
		if(c != null){
			review = new Review();
			review.setId(c.getLong(0));
			review.setVenueId(c.getLong(1));
			review.setComment(c.getString(2));
			review.setTimeStamp(c.getLong(3));
			review.setUserName(c.getString(4));
		}
		return review;
	}

}
